{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# kNN基础"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 准备测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib as mlp\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 解决绘图中文乱码问题\n",
    "plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus']=False #用来正常显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_data_X = [[3.393533211, 2.331273381],\n",
    "              [3.110073483, 1.781539638],\n",
    "              [1.343808831, 3.368360954],\n",
    "              [3.582294042, 4.679179110],\n",
    "              [2.280362439, 2.866990263],\n",
    "              [7.423436942, 4.696522875],\n",
    "              [5.745051997, 3.533989803],\n",
    "              [9.172168622, 2.511101045],\n",
    "              [7.792783481, 3.424088941],\n",
    "              [7.939820817, 0.791637231]]\n",
    "raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = np.array(raw_data_X)\n",
    "y_train = np.array(raw_data_y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.39353321, 2.33127338],\n",
       "       [3.11007348, 1.78153964],\n",
       "       [1.34380883, 3.36836095],\n",
       "       [3.58229404, 4.67917911],\n",
       "       [2.28036244, 2.86699026],\n",
       "       [7.42343694, 4.69652288],\n",
       "       [5.745052  , 3.5339898 ],\n",
       "       [9.17216862, 2.51110105],\n",
       "       [7.79278348, 3.42408894],\n",
       "       [7.93982082, 0.79163723]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 绘图查看数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD6CAYAAAC1W2xyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAE1hJREFUeJzt3XFspPWd3/H3Z2GpMIk2IKxt71rbjURVRbeQUpcDNUF7LES5KKRpdCp/WInU3MnqCV3/yD8QmSgixZEapWilnKCdhlYRnYtKqia6zR0Klw2b0Ctc4j0SNrq26v2Bt6FBcQosJf6jK+7bP2aW9Tpje2zGfsbPvl+SNc/85ueZj3Y1H//8PM/4SVUhSWqPA00HkCSNlsUuSS1jsUtSy1jsktQyFrsktYzFLkktY7FLUstY7JLUMha7JLXMlU286PXXX18zMzNNvLQk7VunT5/+eVVNbjWvkWKfmZlhaWmpiZeWpH0ryfIw89wVI0ktY7FLUstY7JLUMha7JLWMxS5JLWOxS1LLWOyS1DIWu6TR63ZhZgYOHOjddrtNJ7qsWOwCoHumy8zxGQ48eICZ4zN0z/hG1A51uzA/D8vLUNW7nZ+33PeQxS66Z7rMn5hn+dwyRbF8bpn5E/OWu3ZmYQFWVy8dW13tjWtPWOxi4eQCq+cvfSOunl9l4aRv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UF6hflikn/UdJ5BkvwqcLiqlprO0vd/gF9L8i7gbwH/q+E8a/0d4M/72z8DDjUVpLFir6rXq+pcU6+/lSS3AddW1XNNZ1krSej9QHwVON9wHAD6u6s+A9zfdJYBfgDcWVW3AAeBDzWcZ61PAH8BfAG4JcnvNZxnkHuBR5sOscZ/AaaBfw78N+CVZuNc4j8Bn+3vVvsgcLKpIB48HSDJdcCXgE82nWW96rkXeAH4SNN5+u4HHqmq15oOMsALVfXT/vYScEOTYdb5e0Cnql4G/gPwGw3nuUSSA/QynWo4ylqfBf5ZVX0O+O/AP204z1uq6iHgSeB3gK9U1RtNZbHY1+mvPr8GfLqqlreav5eS3JfkE/277wLGpUjvBO5Ncgp4b5IvN5xnrceT3JTkCuCjwI+aDrTGXwLv7m/PcvHvJ42L9wN/VuN1TvS1wJH+/+evA+OUDeCHwBTwcJMhGj+PPcmp/gGasZDkd4HPc7EAHq2q/9hgpLf0Dzg/Afw14MfAvWP2phvH/89fA/4ACPCHVbXQcKS3JHkn8O+Aw/R2E/1WVb3UbKqLknweWKqq/9x0lguS3AL8e3q7Y54F/nGTK+P1kjwI/GVVPd5ojjHrBUnS2+SuGElqGYtdklrGYpeklrHYJallLHZJahmLXZJaxmKXpJb5/yOaXpE+bfnpAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train == 0, 0], X_train[y_train == 0, 1], color = 'g')\n",
    "plt.scatter(X_train[y_train == 1, 0], X_train[y_train == 1, 1], color = 'r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 引入新样本，并在图中绘制查看"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 现有一个新的样本，在图上标位蓝色\n",
    "a = np.array ([8.093607318, 3.365731514])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD6CAYAAAC1W2xyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAE2lJREFUeJzt3X+M3PWd3/Hny2AqlkQOiJXbu9a7jURVRWdI6ZYDNUE+DFEuCmkancofq0Rq7rTqCV3/yD8QLVFEykZqlCJLOUE7Da0iOheVVE10zh0KFwcn9AqXrI8ER9dWvT/YbWhQNgVMyf5Ri3v3jxnj9WZ2d3aZ3e/s18+HtJrvfObjmZdszcsff3/4m6pCktQeB5oOIEkaLYtdklrGYpeklrHYJallLHZJahmLXZJaxmKXpJax2CWpZSx2SWqZK5v40Ouvv76mp6eb+GhJ2rfOnDnz86qa3GpeI8U+PT3N4uJiEx8tSftWkqVh5rkrRpJaxmKXpJax2CWpZSx2SWoZi12SWsZil6SWsdglqWUsdkmj1+3C9DQcONB77HabTnRZsdgFQPdsl+kT0xx48ADTJ6bpnvWLqB3qdmFuDpaWoKr3ODdnue8hi110z3aZOznH0rklimLp3BJzJ+csd+3M/Dysrl46trraG9eesNjF/Kl5Vs9f+kVcPb/K/Cm/iNqB5eXtjWvkLHaxfG7wF26jcWlTR45sb1wjZ7GLI4cGf+E2Gpc2tbAAExOXjk1M9Ma1Jyx2sXB8gYmDl34RJw5OsHDcL6J2YHYWOh2YmoKk99jp9Ma1Jxr5b3s1XmaP9r5w86fmWT63zJFDR1g4vvDWuLRts7MWeYNSVXv+oTMzM+X/xy5J25PkTFXNbDXPXTGS1DJDFXuSw0me3+C1K5MsJznd/zk62oiSpO0Ydh/7F4GrN3jtRuCrVXXfaCJJkt6OLVfsSe4AfgG8vMGUW4EPJ/l+kseSeEBWkhq0abEnuQr4D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C8JGm8/TdDzxSVa81HWSAF6rqp/3tReCGJsOs8/eATlW9DPwH4DcaznOJJAfoZTrdcJS1Pgv8s6r6HPDfgX/acJ63VNVDwJPA7wBfqao3mspisa/TX31+Dfh0VS1tNX8vJbkvySf6T98FjEuR3gncm+Q08N4kX244z1qPJ7kpyRXAR4EfNR1ojb8E3t3fnuHi/580Lt4P/FmN1znR1wJH+3+evw6MUzaAHwJHgIebDNH4eexJTvcP0IyFJL8LfJ6LBfBoVf3HBiO9pX/A+QngrwE/Bu4dsy/dOP55/hrwB0CAP6yq+YYjvSXJO4F/Bxymt5vot6rqpWZTXZTk88BiVf3nprNckOQW4N/T2x3zLPCPm1wZr5fkQeAvq+rxRnOMWS9Ikt4md8VIUstY7JLUMha7JLWMxS5JLWOxS1LLWOyS1DIWuyS1zP8HCBN8CMvDNwYAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train == 0, 0], X_train[y_train == 0, 1], color = 'g')\n",
    "plt.scatter(X_train[y_train == 1, 0], X_train[y_train == 1, 1], color = 'r')\n",
    "plt.scatter(a[0], a[1], color = 'b')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用KNN算法预测蓝色点应该归为红色还是蓝色"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.09360732, 3.36573151])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算样本a到每一个训练样本的距离"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.81256691, 5.22927083, 6.749799  , 4.69862661, 5.83460015,\n",
       "       1.4900114 , 2.3545749 , 1.37611327, 0.306432  , 2.5786841 ])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 写法1：\n",
    "dis = []\n",
    "for x in X_train:\n",
    "#     d = np.sqrt((a[0] - x[0]) ** 2 + (a[1] - x[1]) ** 2)\n",
    "    d = np.sqrt(np.sum((a - x) ** 2))\n",
    "    dis.append(d)\n",
    "distances = np.array(dis)\n",
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4.81256691, 5.22927083, 6.749799  , 4.69862661, 5.83460015,\n",
       "       1.4900114 , 2.3545749 , 1.37611327, 0.306432  , 2.5786841 ])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 写法2：\n",
    "dis = [np.sqrt(np.sum((a - x) ** 2)) for x in X_train]\n",
    "distances = np.array(dis)\n",
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 7, 5], dtype=int64)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 指定 k = 3\n",
    "# 找出离a最近的k个样本点的行索引\n",
    "nearestDisIndexes = distances.argsort()[:3]\n",
    "nearestDisIndexes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计训练样本类型中的所有的样本类型\n",
    "y_type = np.unique(y_train)\n",
    "y_type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 3]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 统计每一种类型出现的次数\n",
    "counts = [np.sum(y_train[nearestDisIndexes] == i) for i in y_type]\n",
    "counts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(counts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回 距离样本a最近的 k个样本中 出现次数最多的类型 作为KNN算法预测的结果\n",
    "a_result = y_type[np.argmax(counts)]\n",
    "a_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'r'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果样本a的是属于类别0的，那么就是用绿色，否则就是用红色\n",
    "a_color = 'g' if a_result == 0 else 'r'\n",
    "a_color"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXYAAAD6CAYAAAC1W2xyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAE2lJREFUeJzt3X+M3PWd3/Hny2AqlkQOiJXbu9a7jURVRWdI6ZYDNUE+DFEuCmkancofq0Rq7rTqCV3/yD8QLVFEykZqlCJLOUE7Da0iOheVVE10zh0KFw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QeAvq+rxRnOMWS9Ikt4md8VIUstY7JLUMha7JLWMxS5JLWOxS1LLWOyS1DIWuyS1zP8H7VR8CHYhmxQAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 根据KNN算法的预测结果绘制散点图，查看样本a的颜色\n",
    "plt.scatter(X_train[y_train == 0, 0], X_train[y_train == 0, 1], color = 'g')\n",
    "plt.scatter(X_train[y_train == 1, 0], X_train[y_train == 1, 1], color = 'r')\n",
    "plt.scatter(a[0], a[1], color = a_color)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将kNN算法代码封装到一个py脚本文件中，然后尝试调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.39353321, 2.33127338],\n",
       "       [3.11007348, 1.78153964],\n",
       "       [1.34380883, 3.36836095],\n",
       "       [3.58229404, 4.67917911],\n",
       "       [2.28036244, 2.86699026],\n",
       "       [7.42343694, 4.69652288],\n",
       "       [5.745052  , 3.5339898 ],\n",
       "       [9.17216862, 2.51110105],\n",
       "       [7.79278348, 3.42408894],\n",
       "       [7.93982082, 0.79163723]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.09360732, 3.36573151])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run kNN_function/kNN1.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kNN_classify1(3, X_train, y_train, a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "%run kNN_function/kNN2.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kNN_classify2(3, X_train, y_train, a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用scikit-learn中的kNN算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.39353321, 2.33127338],\n",
       "       [3.11007348, 1.78153964],\n",
       "       [1.34380883, 3.36836095],\n",
       "       [3.58229404, 4.67917911],\n",
       "       [2.28036244, 2.86699026],\n",
       "       [7.42343694, 4.69652288],\n",
       "       [5.745052  , 3.5339898 ],\n",
       "       [9.17216862, 2.51110105],\n",
       "       [7.79278348, 3.42408894],\n",
       "       [7.93982082, 0.79163723]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.09360732, 3.36573151])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "kNN_classifier = KNeighborsClassifier(n_neighbors = 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=None, n_neighbors=6, p=2,\n",
       "           weights='uniform')"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kNN_classifier.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 一次需要传入一个矩阵（包含多个待预测的样本），\n",
    "# 我这里就一条样本a，因此需要先将向量a转成矩阵\n",
    "kNN_classifier.predict(a.reshape(1, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模仿scikit-learn中的kNN算法，封装完善我们自己的kNN算法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[3.39353321, 2.33127338],\n",
       "       [3.11007348, 1.78153964],\n",
       "       [1.34380883, 3.36836095],\n",
       "       [3.58229404, 4.67917911],\n",
       "       [2.28036244, 2.86699026],\n",
       "       [7.42343694, 4.69652288],\n",
       "       [5.745052  , 3.5339898 ],\n",
       "       [9.17216862, 2.51110105],\n",
       "       [7.79278348, 3.42408894],\n",
       "       [7.93982082, 0.79163723]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8.09360732, 3.36573151])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试kNN1_1\n",
    "%run kNN_function/kNN1_1.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "kNNClassifier1_1 = KNNClassifier1_1(k = 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.KNNClassifier1_1 at 0x151cb2bc978>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kNNClassifier1_1.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_predict = kNNClassifier1_1.predict(a.reshape(1, 2))\n",
    "y_predict[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试kNN2_1\n",
    "%run kNN_function/kNN2_1.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "kNNClassifier2_1 = KNNClassifier2_1(k = 6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.KNNClassifier2_1 at 0x151cb2e3b00>"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "kNNClassifier2_1.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_predict = kNNClassifier2_1.predict(a.reshape(1, 2))\n",
    "y_predict[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 训练数据集和预测数据集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 准备数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 解决绘图中文乱码问题\n",
    "plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签\n",
    "plt.rcParams['axes.unicode_minus']=False #用来正常显示负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "iris = datasets.load_iris()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = iris.data\n",
    "y = iris.target"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将数据集分割成训练数据集和预测数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看数据集的结果，相同结果的样本数据都聚集在一起，所以需要先打乱顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "150"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 66,  46, 123,  61, 134,  29,  38,  17,  37,  73,   4, 131,  34,\n",
       "        71,  10, 129, 142,  40, 137,  82, 102, 144, 113, 132,  57, 133,\n",
       "        25,  81, 110,  47,  24, 143,  56,  77,  43,  93,  89,  87,  88,\n",
       "        41, 105, 109, 148,  95,  15,  74, 104, 145,  44,  14, 146,  79,\n",
       "        59,  18, 101, 114,  39, 106, 135, 116,  85, 141,  13,  33,  94,\n",
       "        53, 111,  31,  30,  98,  27,  50,  58,   3, 130,  83, 149,  92,\n",
       "        20, 122,   7,  28,  21, 108,  67,  26, 107, 128,  45,  72, 140,\n",
       "        75, 127,  84,  62,  23,  48, 112,   6,  69, 139, 125,  11, 126,\n",
       "       138,   1,  64, 121,  52,  60,  55, 103,   8,  86,  91,  19,  42,\n",
       "         2,  51,  49,   0,  32,  70,  22, 118,  99,  76,  35, 124, 115,\n",
       "         5,   9,  65, 147,  16,  80,  68, 136, 120,  96, 117,  54, 100,\n",
       "        63,  90,  78,  36,  97, 119,  12])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成1-150内的所有元素，并且随机打乱顺序，作为索引\n",
    "shuffle_indexes = np.random.permutation(len(X))\n",
    "shuffle_indexes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_ratio = 0.2\n",
    "test_size = int(len(X) * test_ratio)\n",
    "test_size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 66,  46, 123,  61, 134,  29,  38,  17,  37,  73,   4, 131,  34,\n",
       "        71,  10, 129, 142,  40, 137,  82, 102, 144, 113, 132,  57, 133,\n",
       "        25,  81, 110,  47])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_indexes = shuffle_indexes[:test_size]\n",
    "test_indexes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 24, 143,  56,  77,  43,  93,  89,  87,  88,  41, 105, 109, 148,\n",
       "        95,  15,  74, 104, 145,  44,  14, 146,  79,  59,  18, 101, 114,\n",
       "        39, 106, 135, 116,  85, 141,  13,  33,  94,  53, 111,  31,  30,\n",
       "        98,  27,  50,  58,   3, 130,  83, 149,  92,  20, 122,   7,  28,\n",
       "        21, 108,  67,  26, 107, 128,  45,  72, 140,  75, 127,  84,  62,\n",
       "        23,  48, 112,   6,  69, 139, 125,  11, 126, 138,   1,  64, 121,\n",
       "        52,  60,  55, 103,   8,  86,  91,  19,  42,   2,  51,  49,   0,\n",
       "        32,  70,  22, 118,  99,  76,  35, 124, 115,   5,   9,  65, 147,\n",
       "        16,  80,  68, 136, 120,  96, 117,  54, 100,  63,  90,  78,  36,\n",
       "        97, 119,  12])"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_indexes = shuffle_indexes[test_size:]\n",
    "train_indexes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 封装以上代码到一个py脚本中进行调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "from common.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(120, 4)"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(120,)"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(30, 4)"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(30,)"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "from kNN_function.kNN2_1 import KNNClassifier2_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_knn_clf2_1 = KNNClassifier2_1(k = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<kNN_function.kNN2_1.KNNClassifier2_1 at 0x151cb487fd0>"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_knn_clf2_1.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 2, 1, 2, 0, 2, 0, 0, 1, 1, 1, 2, 1, 0, 0, 1, 1, 2, 2, 1, 0,\n",
       "       0, 0, 2, 0, 0, 2, 0, 0])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_predict = my_knn_clf2_1.predict(X_test)\n",
    "y_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 2, 1, 2, 0, 2, 0, 0, 1, 1, 1, 2, 1, 0, 0, 1, 1, 2, 2, 1, 0,\n",
       "       0, 0, 2, 0, 0, 2, 0, 0])"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(y_predict == y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(y_predict == y_test) / len(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(120, 4)"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(120,)"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(30, 4)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(30,)"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2],\n",
       "       [4.9, 3. , 1.4, 0.2],\n",
       "       [5.7, 2.8, 4.1, 1.3],\n",
       "       [6.2, 3.4, 5.4, 2.3],\n",
       "       [5.1, 2.5, 3. , 1.1],\n",
       "       [7. , 3.2, 4.7, 1.4],\n",
       "       [6.1, 2.6, 5.6, 1.4],\n",
       "       [7.6, 3. , 6.6, 2.1],\n",
       "       [5.2, 4.1, 1.5, 0.1],\n",
       "       [6.2, 2.2, 4.5, 1.5],\n",
       "       [7.3, 2.9, 6.3, 1.8],\n",
       "       [6.4, 3.2, 5.3, 2.3],\n",
       "       [6. , 3.4, 4.5, 1.6],\n",
       "       [5.2, 2.7, 3.9, 1.4],\n",
       "       [5.4, 3.7, 1.5, 0.2],\n",
       "       [5.3, 3.7, 1.5, 0.2],\n",
       "       [5. , 3.5, 1.6, 0.6],\n",
       "       [4.4, 2.9, 1.4, 0.2],\n",
       "       [5.8, 2.7, 3.9, 1.2],\n",
       "       [5.2, 3.4, 1.4, 0.2],\n",
       "       [4.6, 3.4, 1.4, 0.3],\n",
       "       [6.5, 3.2, 5.1, 2. ],\n",
       "       [5.7, 2.9, 4.2, 1.3],\n",
       "       [6.6, 3. , 4.4, 1.4],\n",
       "       [6. , 2.9, 4.5, 1.5],\n",
       "       [4.7, 3.2, 1.6, 0.2],\n",
       "       [4.9, 3.6, 1.4, 0.1],\n",
       "       [6.7, 3.1, 5.6, 2.4],\n",
       "       [6.3, 2.7, 4.9, 1.8],\n",
       "       [6.1, 2.8, 4.7, 1.2],\n",
       "       [6.2, 2.8, 4.8, 1.8],\n",
       "       [5.7, 4.4, 1.5, 0.4],\n",
       "       [6.3, 2.5, 4.9, 1.5],\n",
       "       [6.4, 2.9, 4.3, 1.3],\n",
       "       [5.1, 3.8, 1.9, 0.4],\n",
       "       [6.8, 2.8, 4.8, 1.4],\n",
       "       [5.1, 3.5, 1.4, 0.3],\n",
       "       [4.3, 3. , 1.1, 0.1],\n",
       "       [5.9, 3. , 5.1, 1.8],\n",
       "       [6.4, 2.8, 5.6, 2.1],\n",
       "       [5.6, 2.8, 4.9, 2. ],\n",
       "       [5.5, 2.4, 3.7, 1. ],\n",
       "       [6.9, 3.1, 4.9, 1.5],\n",
       "       [6.3, 3.4, 5.6, 2.4],\n",
       "       [6.5, 3. , 5.8, 2.2],\n",
       "       [5.7, 3.8, 1.7, 0.3],\n",
       "       [6.6, 2.9, 4.6, 1.3],\n",
       "       [6.7, 3.3, 5.7, 2.5],\n",
       "       [4.8, 3. , 1.4, 0.1],\n",
       "       [5. , 3.6, 1.4, 0.2],\n",
       "       [6. , 3. , 4.8, 1.8],\n",
       "       [5. , 3. , 1.6, 0.2],\n",
       "       [6.1, 3. , 4.9, 1.8],\n",
       "       [6.4, 3.1, 5.5, 1.8],\n",
       "       [5.1, 3.7, 1.5, 0.4],\n",
       "       [5.7, 3. , 4.2, 1.2],\n",
       "       [5. , 3.5, 1.3, 0.3],\n",
       "       [5.1, 3.8, 1.5, 0.3],\n",
       "       [4.6, 3.2, 1.4, 0.2],\n",
       "       [5.4, 3. , 4.5, 1.5],\n",
       "       [6.5, 2.8, 4.6, 1.5],\n",
       "       [6.1, 2.8, 4. , 1.3],\n",
       "       [4.8, 3.4, 1.6, 0.2],\n",
       "       [5.1, 3.3, 1.7, 0.5],\n",
       "       [5.8, 2.7, 5.1, 1.9],\n",
       "       [5.6, 3. , 4.1, 1.3],\n",
       "       [6.3, 2.8, 5.1, 1.5],\n",
       "       [6.7, 3.3, 5.7, 2.1],\n",
       "       [7.2, 3. , 5.8, 1.6],\n",
       "       [6.7, 3. , 5.2, 2.3],\n",
       "       [4.9, 3.1, 1.5, 0.1],\n",
       "       [4.9, 2.5, 4.5, 1.7],\n",
       "       [5.7, 2.5, 5. , 2. ],\n",
       "       [4.9, 2.4, 3.3, 1. ],\n",
       "       [5.5, 2.4, 3.8, 1.1],\n",
       "       [6.7, 2.5, 5.8, 1.8],\n",
       "       [6.5, 3. , 5.5, 1.8],\n",
       "       [5.7, 2.6, 3.5, 1. ],\n",
       "       [6. , 2.2, 5. , 1.5],\n",
       "       [6.7, 3.1, 4.7, 1.5],\n",
       "       [4.9, 3.1, 1.5, 0.2],\n",
       "       [6.3, 2.5, 5. , 1.9],\n",
       "       [4.7, 3.2, 1.3, 0.2],\n",
       "       [6. , 2.7, 5.1, 1.6],\n",
       "       [5.8, 2.6, 4. , 1.2],\n",
       "       [6.9, 3.2, 5.7, 2.3],\n",
       "       [4.4, 3. , 1.3, 0.2],\n",
       "       [7.1, 3. , 5.9, 2.1],\n",
       "       [6.8, 3.2, 5.9, 2.3],\n",
       "       [5.2, 3.5, 1.5, 0.2],\n",
       "       [7.9, 3.8, 6.4, 2. ],\n",
       "       [6.4, 2.8, 5.6, 2.2],\n",
       "       [5.6, 2.9, 3.6, 1.3],\n",
       "       [5.5, 4.2, 1.4, 0.2],\n",
       "       [4.8, 3. , 1.4, 0.3],\n",
       "       [6.3, 2.9, 5.6, 1.8],\n",
       "       [4.4, 3.2, 1.3, 0.2],\n",
       "       [5.4, 3.9, 1.7, 0.4],\n",
       "       [5. , 3.4, 1.6, 0.4],\n",
       "       [6.7, 3. , 5. , 1.7],\n",
       "       [5.4, 3.9, 1.3, 0.4],\n",
       "       [7.7, 2.8, 6.7, 2. ],\n",
       "       [5.6, 2.7, 4.2, 1.3],\n",
       "       [6.8, 3. , 5.5, 2.1],\n",
       "       [5.5, 3.5, 1.3, 0.2],\n",
       "       [4.8, 3.4, 1.9, 0.2],\n",
       "       [5.4, 3.4, 1.7, 0.2],\n",
       "       [7.7, 3.8, 6.7, 2.2],\n",
       "       [6.9, 3.1, 5.4, 2.1],\n",
       "       [5.6, 2.5, 3.9, 1.1],\n",
       "       [5.1, 3.4, 1.5, 0.2],\n",
       "       [5.1, 3.8, 1.6, 0.2],\n",
       "       [6.1, 2.9, 4.7, 1.4],\n",
       "       [5.8, 4. , 1.2, 0.2],\n",
       "       [5. , 2. , 3.5, 1. ],\n",
       "       [6.4, 3.2, 4.5, 1.5],\n",
       "       [6.1, 3. , 4.6, 1.4],\n",
       "       [5.9, 3.2, 4.8, 1.8],\n",
       "       [6. , 2.2, 4. , 1. ],\n",
       "       [7.4, 2.8, 6.1, 1.9]])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 计算分类准确度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 准备手写数字数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "digits = datasets.load_digits()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['data', 'target', 'target_names', 'images', 'DESCR'])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".. _digits_dataset:\n",
      "\n",
      "Optical recognition of handwritten digits dataset\n",
      "--------------------------------------------------\n",
      "\n",
      "**Data Set Characteristics:**\n",
      "\n",
      "    :Number of Instances: 5620\n",
      "    :Number of Attributes: 64\n",
      "    :Attribute Information: 8x8 image of integer pixels in the range 0..16.\n",
      "    :Missing Attribute Values: None\n",
      "    :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)\n",
      "    :Date: July; 1998\n",
      "\n",
      "This is a copy of the test set of the UCI ML hand-written digits datasets\n",
      "http://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits\n",
      "\n",
      "The data set contains images of hand-written digits: 10 classes where\n",
      "each class refers to a digit.\n",
      "\n",
      "Preprocessing programs made available by NIST were used to extract\n",
      "normalized bitmaps of handwritten digits from a preprinted form. From a\n",
      "total of 43 people, 30 contributed to the training set and different 13\n",
      "to the test set. 32x32 bitmaps are divided into nonoverlapping blocks of\n",
      "4x4 and the number of on pixels are counted in each block. This generates\n",
      "an input matrix of 8x8 where each element is an integer in the range\n",
      "0..16. This reduces dimensionality and gives invariance to small\n",
      "distortions.\n",
      "\n",
      "For info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.\n",
      "T. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.\n",
      "L. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,\n",
      "1994.\n",
      "\n",
      ".. topic:: References\n",
      "\n",
      "  - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their\n",
      "    Applications to Handwritten Digit Recognition, MSc Thesis, Institute of\n",
      "    Graduate Studies in Science and Engineering, Bogazici University.\n",
      "  - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.\n",
      "  - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.\n",
      "    Linear dimensionalityreduction using relevance weighted LDA. School of\n",
      "    Electrical and Electronic Engineering Nanyang Technological University.\n",
      "    2005.\n",
      "  - Claudio Gentile. A New Approximate Maximal Margin Classification\n",
      "    Algorithm. NIPS. 2000.\n"
     ]
    }
   ],
   "source": [
    "print(digits.DESCR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797, 64)"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = digits.data\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1797,)"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = digits.target\n",
    "y.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digits.target_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1,\n",
       "       2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 5, 5, 6, 5, 0, 9, 8, 9, 8, 4, 1, 7,\n",
       "       7, 3, 5, 1, 0, 0, 2, 2, 7, 8, 2, 0, 1, 2, 6, 3, 3, 7, 3, 3, 4, 6,\n",
       "       6, 6, 4, 9, 1, 5, 0, 9, 5, 2, 8, 2, 0, 0, 1, 7, 6, 3, 2, 1, 7, 4,\n",
       "       6, 3, 1, 3, 9, 1, 7, 6, 8, 4, 3, 1])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y[:100]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  0.,  5., 13.,  9.,  1.,  0.,  0.,  0.,  0., 13., 15., 10.,\n",
       "        15.,  5.,  0.,  0.,  3., 15.,  2.,  0., 11.,  8.,  0.,  0.,  4.,\n",
       "        12.,  0.,  0.,  8.,  8.,  0.,  0.,  5.,  8.,  0.,  0.,  9.,  8.,\n",
       "         0.,  0.,  4., 11.,  0.,  1., 12.,  7.,  0.,  0.,  2., 14.,  5.,\n",
       "        10., 12.,  0.,  0.,  0.,  0.,  6., 13., 10.,  0.,  0.,  0.],\n",
       "       [ 0.,  0.,  0., 12., 13.,  5.,  0.,  0.,  0.,  0.,  0., 11., 16.,\n",
       "         9.,  0.,  0.,  0.,  0.,  3., 15., 16.,  6.,  0.,  0.,  0.,  7.,\n",
       "        15., 16., 16.,  2.,  0.,  0.,  0.,  0.,  1., 16., 16.,  3.,  0.,\n",
       "         0.,  0.,  0.,  1., 16., 16.,  6.,  0.,  0.,  0.,  0.,  1., 16.,\n",
       "        16.,  6.,  0.,  0.,  0.,  0.,  0., 11., 16., 10.,  0.,  0.],\n",
       "       [ 0.,  0.,  0.,  4., 15., 12.,  0.,  0.,  0.,  0.,  3., 16., 15.,\n",
       "        14.,  0.,  0.,  0.,  0.,  8., 13.,  8., 16.,  0.,  0.,  0.,  0.,\n",
       "         1.,  6., 15., 11.,  0.,  0.,  0.,  1.,  8., 13., 15.,  1.,  0.,\n",
       "         0.,  0.,  9., 16., 16.,  5.,  0.,  0.,  0.,  0.,  3., 13., 16.,\n",
       "        16., 11.,  5.,  0.,  0.,  0.,  0.,  3., 11., 16.,  9.,  0.],\n",
       "       [ 0.,  0.,  7., 15., 13.,  1.,  0.,  0.,  0.,  8., 13.,  6., 15.,\n",
       "         4.,  0.,  0.,  0.,  2.,  1., 13., 13.,  0.,  0.,  0.,  0.,  0.,\n",
       "         2., 15., 11.,  1.,  0.,  0.,  0.,  0.,  0.,  1., 12., 12.,  1.,\n",
       "         0.,  0.,  0.,  0.,  0.,  1., 10.,  8.,  0.,  0.,  0.,  8.,  4.,\n",
       "         5., 14.,  9.,  0.,  0.,  0.,  7., 13., 13.,  9.,  0.,  0.],\n",
       "       [ 0.,  0.,  0.,  1., 11.,  0.,  0.,  0.,  0.,  0.,  0.,  7.,  8.,\n",
       "         0.,  0.,  0.,  0.,  0.,  1., 13.,  6.,  2.,  2.,  0.,  0.,  0.,\n",
       "         7., 15.,  0.,  9.,  8.,  0.,  0.,  5., 16., 10.,  0., 16.,  6.,\n",
       "         0.,  0.,  4., 15., 16., 13., 16.,  1.,  0.,  0.,  0.,  0.,  3.,\n",
       "        15., 10.,  0.,  0.,  0.,  0.,  0.,  2., 16.,  4.,  0.,  0.],\n",
       "       [ 0.,  0., 12., 10.,  0.,  0.,  0.,  0.,  0.,  0., 14., 16., 16.,\n",
       "        14.,  0.,  0.,  0.,  0., 13., 16., 15., 10.,  1.,  0.,  0.,  0.,\n",
       "        11., 16., 16.,  7.,  0.,  0.,  0.,  0.,  0.,  4.,  7., 16.,  7.,\n",
       "         0.,  0.,  0.,  0.,  0.,  4., 16.,  9.,  0.,  0.,  0.,  5.,  4.,\n",
       "        12., 16.,  4.,  0.,  0.,  0.,  9., 16., 16., 10.,  0.,  0.],\n",
       "       [ 0.,  0.,  0., 12., 13.,  0.,  0.,  0.,  0.,  0.,  5., 16.,  8.,\n",
       "         0.,  0.,  0.,  0.,  0., 13., 16.,  3.,  0.,  0.,  0.,  0.,  0.,\n",
       "        14., 13.,  0.,  0.,  0.,  0.,  0.,  0., 15., 12.,  7.,  2.,  0.,\n",
       "         0.,  0.,  0., 13., 16., 13., 16.,  3.,  0.,  0.,  0.,  7., 16.,\n",
       "        11., 15.,  8.,  0.,  0.,  0.,  1.,  9., 15., 11.,  3.,  0.],\n",
       "       [ 0.,  0.,  7.,  8., 13., 16., 15.,  1.,  0.,  0.,  7.,  7.,  4.,\n",
       "        11., 12.,  0.,  0.,  0.,  0.,  0.,  8., 13.,  1.,  0.,  0.,  4.,\n",
       "         8.,  8., 15., 15.,  6.,  0.,  0.,  2., 11., 15., 15.,  4.,  0.,\n",
       "         0.,  0.,  0.,  0., 16.,  5.,  0.,  0.,  0.,  0.,  0.,  9., 15.,\n",
       "         1.,  0.,  0.,  0.,  0.,  0., 13.,  5.,  0.,  0.,  0.,  0.],\n",
       "       [ 0.,  0.,  9., 14.,  8.,  1.,  0.,  0.,  0.,  0., 12., 14., 14.,\n",
       "        12.,  0.,  0.,  0.,  0.,  9., 10.,  0., 15.,  4.,  0.,  0.,  0.,\n",
       "         3., 16., 12., 14.,  2.,  0.,  0.,  0.,  4., 16., 16.,  2.,  0.,\n",
       "         0.,  0.,  3., 16.,  8., 10., 13.,  2.,  0.,  0.,  1., 15.,  1.,\n",
       "         3., 16.,  8.,  0.,  0.,  0., 11., 16., 15., 11.,  1.,  0.],\n",
       "       [ 0.,  0., 11., 12.,  0.,  0.,  0.,  0.,  0.,  2., 16., 16., 16.,\n",
       "        13.,  0.,  0.,  0.,  3., 16., 12., 10., 14.,  0.,  0.,  0.,  1.,\n",
       "        16.,  1., 12., 15.,  0.,  0.,  0.,  0., 13., 16.,  9., 15.,  2.,\n",
       "         0.,  0.,  0.,  0.,  3.,  0.,  9., 11.,  0.,  0.,  0.,  0.,  0.,\n",
       "         9., 15.,  4.,  0.,  0.,  0.,  9., 12., 13.,  3.,  0.,  0.]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.,  0.,  5., 15., 14.,  3.,  0.,  0.,  0.,  0., 13., 15.,  9.,\n",
       "       15.,  2.,  0.,  0.,  4., 16., 12.,  0., 10.,  6.,  0.,  0.,  8.,\n",
       "       16.,  9.,  0.,  8., 10.,  0.,  0.,  7., 15.,  5.,  0., 12., 11.,\n",
       "        0.,  0.,  7., 13.,  0.,  5., 16.,  6.,  0.,  0.,  0., 16., 12.,\n",
       "       15., 13.,  1.,  0.,  0.,  0.,  6., 16., 12.,  2.,  0.,  0.])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[666]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y[666]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.,  0.,  5., 15., 14.,  3.,  0.,  0.],\n",
       "       [ 0.,  0., 13., 15.,  9., 15.,  2.,  0.],\n",
       "       [ 0.,  4., 16., 12.,  0., 10.,  6.,  0.],\n",
       "       [ 0.,  8., 16.,  9.,  0.,  8., 10.,  0.],\n",
       "       [ 0.,  7., 15.,  5.,  0., 12., 11.,  0.],\n",
       "       [ 0.,  7., 13.,  0.,  5., 16.,  6.,  0.],\n",
       "       [ 0.,  0., 16., 12., 15., 13.,  1.,  0.],\n",
       "       [ 0.,  0.,  6., 16., 12.,  2.,  0.,  0.]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "digit_image_matrix = X[666].reshape(8, 8)\n",
    "digit_image_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPcAAAD6CAYAAAB9EtGNAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAACh5JREFUeJzt3U+IndUZx/HfzzFC1DYTSRppFx0iLqujDMGAmokYq6U2QSwpiPFPS0sJbroxtoq0iy4kiCAkkBqTUklbXbSphdK0pTdmYdpOMJEiFFwkhdBgxIxWNy3l6eLekjClua+Z8573naffDwh3cmfmPBfy9cy9eeceR4QA5HNZ1wMAaAdxA0kRN5AUcQNJETeQFHEDSRE3kBRxA0kRN5D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/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(digit_image_matrix, cmap = mlp.cm.binary)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将手写数字数据集分割成训练数据集合预测数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "from common.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_ratio = 0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1438, 64)"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1438,)"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(359, 64)"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(359,)"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 调用自己封装的预测准确度函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [],
   "source": [
    "from kNN_function.kNN2_1 import KNNClassifier2_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<kNN_function.kNN2_1.KNNClassifier2_1 at 0x151cc6e0f28>"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_knn_clf2_1 = KNNClassifier2_1(k = 4)\n",
    "my_knn_clf2_1.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_predict = my_knn_clf2_1.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9916434540389972"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 识别准确度\n",
    "sum(y_predict == y_test) / len(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9916434540389972"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用封装后的计算准确率的函数\n",
    "from common.metrics import accuracy_score\n",
    "accuracy_score(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9916434540389972"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_knn_clf2_1.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 调用scikit-learn中的accuracy_score方法计算预测准确度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1437, 64)"
      ]
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1437,)"
      ]
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(360, 64)"
      ]
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(360,)"
      ]
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "knn_clf = KNeighborsClassifier(n_neighbors = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=None, n_neighbors=3, p=2,\n",
       "           weights='uniform')"
      ]
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_predict = knn_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.metrics import accuracy_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9888888888888889"
      ]
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "accuracy_score(y_test, y_predict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9888888888888889"
      ]
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 超参数问题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "超参数决定我们后面算法识别的准确度，比如KNN算法中的k的值就是一个超参数，是否考虑待测样本点到其它样本点之间的距离也是超参数。\n",
    "\n",
    "寻找好的超参数的方法：\n",
    "* 领域知识\n",
    "* 经验数值\n",
    "* 实验搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 寻找好的k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 0.9833333333333333\n",
      "2 0.9888888888888889\n",
      "3 0.9888888888888889\n",
      "4 0.9916666666666667\n",
      "5 0.9888888888888889\n",
      "6 0.9888888888888889\n",
      "7 0.9861111111111112\n",
      "8 0.9861111111111112\n",
      "9 0.9833333333333333\n",
      "10 0.9833333333333333\n"
     ]
    }
   ],
   "source": [
    "best_score = 0.0\n",
    "best_k = -1\n",
    "for k in range(1, 11):\n",
    "    knn_clf = KNeighborsClassifier(n_neighbors = k)\n",
    "    knn_clf.fit(X_train, y_train)\n",
    "    score = knn_clf.score(X_test, y_test)\n",
    "    print(k, score)\n",
    "    if score > best_score:\n",
    "        best_k = k\n",
    "        best_score = score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "求得k为4的时候分类准确度最高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 184,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9916666666666667"
      ]
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 是否考虑距离所占的权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "metadata": {},
   "outputs": [],
   "source": [
    "best_method = \"\"\n",
    "best_score = 0.0\n",
    "best_k = k\n",
    "for k in range(1, 11):\n",
    "    for method in [\"uniform\", \"distance\"]:\n",
    "        knn_clf = KNeighborsClassifier(n_neighbors = k, weights = method) \n",
    "        knn_clf.fit(X_train, y_train)\n",
    "        score = knn_clf.score(X_test, y_test)\n",
    "        if score > best_score:\n",
    "            best_k = k\n",
    "            best_method = method\n",
    "            best_score = score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 213,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 213,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 214,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'uniform'"
      ]
     },
     "execution_count": 214,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 在当前训练样本中不考虑距离所占权重分类准确率更高\n",
    "best_method"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 215,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9916666666666667"
      ]
     },
     "execution_count": 215,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 距离的定义"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$$\n",
    "$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "明可夫斯基距离\n",
    "\n",
    "  $(\\sum\\limits_{i=1}^{n}|X_i^{(a)}-X_i^{(b)}|^{p})^\\frac{1}{p}$\n",
    "   \n",
    "* p为1时，为曼哈顿距离\n",
    "\n",
    "  $\\sum\\limits_{i=1}^{n}|X_i^{(a)}-X_i^{(b)}|$\n",
    "  \n",
    "* p为2时，为欧拉距离\n",
    "\n",
    "  $(\\sum\\limits_{i=1}^{n}|X_i^{(a)}-X_i^{(b)}|^{2})^\\frac{1}{2}$\n",
    "\n",
    "  即：\n",
    "\n",
    "  $\\sqrt{\\sum\\limits_{i=1}^{n}(X_i^{(a)}-X_i^{(b)})^{2}}$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 寻找好的p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 222,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 22.6 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "best_k = -1\n",
    "best_p = -1\n",
    "best_score = 0.0\n",
    " # 寻找p的时候需要指定weights为distance\n",
    "for k in range(1, 11):\n",
    "    for p in range(1, 6):\n",
    "        knn_clf = KNeighborsClassifier(n_neighbors = k, weights = \"distance\", p = p)\n",
    "        knn_clf.fit(X_train, y_train)\n",
    "        score = knn_clf.score(X_test, y_test)\n",
    "        if score > best_score:\n",
    "            best_k = k\n",
    "            best_p = p\n",
    "            best_score = score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 218,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 218,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 219,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 219,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 220,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9888888888888889"
      ]
     },
     "execution_count": 220,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "best_score"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用sciket-learn的Grid Search（网格搜索）的方式寻找最佳超参数组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 224,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_grid = [\n",
    "    {\n",
    "        \"weights\": [\"uniform\"],\n",
    "        \"n_neighbors\": [i for i in range(1, 11)]\n",
    "    },\n",
    "    {\n",
    "        \"weights\": [\"distance\"],\n",
    "        \"n_neighbors\": [i for i in range(1, 11)],\n",
    "        \"p\": [i for i in range(1, 6)]\n",
    "    }    \n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 225,
   "metadata": {},
   "outputs": [],
   "source": [
    "knn_clf = KNeighborsClassifier()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import GridSearchCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 227,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid_search = GridSearchCV(knn_clf, param_grid)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\CandyWall\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_split.py:2053: FutureWarning: You should specify a value for 'cv' instead of relying on the default value. The default value will change from 3 to 5 in version 0.22.\n",
      "  warnings.warn(CV_WARNING, FutureWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 3min 12s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv='warn', error_score='raise-deprecating',\n",
       "       estimator=KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=None, n_neighbors=5, p=2,\n",
       "           weights='uniform'),\n",
       "       fit_params=None, iid='warn', n_jobs=None,\n",
       "       param_grid=[{'weights': ['uniform'], 'n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}, {'weights': ['distance'], 'n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'p': [1, 2, 3, 4, 5]}],\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n",
       "       scoring=None, verbose=0)"
      ]
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "grid_search.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 229,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=None, n_neighbors=3, p=3,\n",
       "           weights='distance')"
      ]
     },
     "execution_count": 229,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search.best_estimator_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9853862212943633"
      ]
     },
     "execution_count": 230,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 231,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'n_neighbors': 3, 'p': 3, 'weights': 'distance'}"
      ]
     },
     "execution_count": 231,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid_search.best_params_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {},
   "outputs": [],
   "source": [
    "knn_clf = grid_search.best_estimator_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 1, 3, 4, 4, 0, 7, 0, 8, 0, 4, 6, 1, 1, 2, 0, 1, 6, 7, 3, 3, 6,\n",
       "       5, 2, 9, 4, 0, 2, 0, 3, 0, 8, 7, 2, 3, 5, 1, 3, 1, 5, 8, 6, 2, 6,\n",
       "       3, 1, 3, 0, 0, 4, 9, 9, 2, 8, 7, 0, 5, 4, 0, 9, 5, 5, 8, 7, 4, 2,\n",
       "       8, 8, 7, 5, 4, 3, 0, 2, 7, 2, 1, 2, 4, 0, 9, 0, 6, 6, 2, 0, 0, 5,\n",
       "       4, 4, 3, 1, 3, 8, 6, 4, 4, 7, 5, 6, 8, 4, 8, 4, 6, 9, 7, 7, 0, 8,\n",
       "       8, 3, 9, 7, 1, 8, 4, 2, 7, 0, 0, 4, 9, 6, 7, 3, 4, 6, 4, 8, 4, 7,\n",
       "       2, 6, 9, 5, 8, 7, 2, 5, 5, 9, 7, 9, 3, 1, 9, 4, 4, 1, 5, 1, 6, 4,\n",
       "       4, 8, 1, 6, 2, 5, 2, 1, 4, 4, 3, 9, 4, 0, 6, 0, 8, 3, 8, 7, 3, 0,\n",
       "       3, 0, 5, 9, 2, 7, 1, 8, 1, 4, 3, 3, 7, 8, 2, 7, 2, 2, 8, 0, 5, 7,\n",
       "       6, 7, 3, 4, 7, 1, 7, 0, 9, 2, 8, 9, 3, 8, 9, 1, 1, 1, 9, 8, 8, 0,\n",
       "       3, 7, 3, 3, 4, 8, 2, 1, 8, 6, 0, 1, 7, 7, 5, 8, 3, 8, 7, 6, 8, 4,\n",
       "       2, 6, 2, 3, 7, 4, 9, 3, 5, 0, 6, 3, 8, 3, 3, 1, 4, 5, 3, 2, 5, 6,\n",
       "       9, 6, 9, 5, 5, 3, 6, 5, 9, 3, 7, 7, 0, 2, 4, 9, 9, 9, 2, 5, 6, 1,\n",
       "       9, 6, 9, 7, 7, 4, 5, 0, 0, 5, 3, 8, 4, 4, 3, 2, 5, 3, 2, 2, 3, 0,\n",
       "       9, 8, 2, 1, 4, 0, 6, 2, 8, 0, 6, 4, 9, 9, 8, 3, 9, 8, 6, 3, 2, 7,\n",
       "       9, 4, 2, 7, 5, 1, 1, 6, 1, 0, 4, 9, 2, 9, 0, 3, 3, 0, 7, 4, 8, 5,\n",
       "       9, 5, 9, 5, 0, 7, 9, 8])"
      ]
     },
     "execution_count": 233,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 234,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9833333333333333"
      ]
     },
     "execution_count": 234,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.score(X_test, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\CandyWall\\Anaconda3\\lib\\site-packages\\sklearn\\model_selection\\_split.py:2053: FutureWarning: You should specify a value for 'cv' instead of relying on the default value. The default value will change from 3 to 5 in version 0.22.\n",
      "  warnings.warn(CV_WARNING, FutureWarning)\n",
      "[Parallel(n_jobs=-1)]: Using backend LokyBackend with 8 concurrent workers.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 3 folds for each of 60 candidates, totalling 180 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Done  25 tasks      | elapsed:   21.8s\n",
      "[Parallel(n_jobs=-1)]: Done 146 tasks      | elapsed:   53.2s\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 1min 4s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Done 180 out of 180 | elapsed:  1.1min finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "GridSearchCV(cv='warn', error_score='raise-deprecating',\n",
       "       estimator=KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=None, n_neighbors=3, p=3,\n",
       "           weights='distance'),\n",
       "       fit_params=None, iid='warn', n_jobs=-1,\n",
       "       param_grid=[{'weights': ['uniform'], 'n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}, {'weights': ['distance'], 'n_neighbors': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'p': [1, 2, 3, 4, 5]}],\n",
       "       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',\n",
       "       scoring=None, verbose=2)"
      ]
     },
     "execution_count": 236,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "# n_jobs计算时使用的CPU核心数，-1为当前计算机的所以核心数\n",
    "# verbose表示查找超参数的时候输出查找过程，verbose的值越大，输出的信息越详细\n",
    "grid_search = GridSearchCV(knn_clf, param_grid, n_jobs = -1, verbose = 2)\n",
    "grid_search.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据归一化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 最值归一化Normalization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([13, 63, 57, 78, 21, 88, 61, 81,  4, 69, 22, 56, 75, 82, 85, 88,  7,\n",
       "       66, 99, 54, 26, 34, 52, 95, 78, 24, 93, 31, 46,  6, 72,  7, 60, 10,\n",
       "       80, 32, 32, 60,  5, 73, 58, 78, 98, 78, 30, 39, 75, 29, 81, 79, 89,\n",
       "       88, 24,  3, 47, 10, 83, 68, 14,  5, 33, 39, 13, 34, 83,  8, 26, 29,\n",
       "       75, 16, 48, 45, 64,  0,  3, 83, 96,  7, 41, 11, 55, 14, 38, 36, 39,\n",
       "       93, 48,  2,  7, 51, 35, 76,  1, 96, 17, 81, 78, 12,  2, 99])"
      ]
     },
     "execution_count": 240,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.random.randint(0, 100, size = 100)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.13131313, 0.63636364, 0.57575758, 0.78787879, 0.21212121,\n",
       "       0.88888889, 0.61616162, 0.81818182, 0.04040404, 0.6969697 ,\n",
       "       0.22222222, 0.56565657, 0.75757576, 0.82828283, 0.85858586,\n",
       "       0.88888889, 0.07070707, 0.66666667, 1.        , 0.54545455,\n",
       "       0.26262626, 0.34343434, 0.52525253, 0.95959596, 0.78787879,\n",
       "       0.24242424, 0.93939394, 0.31313131, 0.46464646, 0.06060606,\n",
       "       0.72727273, 0.07070707, 0.60606061, 0.1010101 , 0.80808081,\n",
       "       0.32323232, 0.32323232, 0.60606061, 0.05050505, 0.73737374,\n",
       "       0.58585859, 0.78787879, 0.98989899, 0.78787879, 0.3030303 ,\n",
       "       0.39393939, 0.75757576, 0.29292929, 0.81818182, 0.7979798 ,\n",
       "       0.8989899 , 0.88888889, 0.24242424, 0.03030303, 0.47474747,\n",
       "       0.1010101 , 0.83838384, 0.68686869, 0.14141414, 0.05050505,\n",
       "       0.33333333, 0.39393939, 0.13131313, 0.34343434, 0.83838384,\n",
       "       0.08080808, 0.26262626, 0.29292929, 0.75757576, 0.16161616,\n",
       "       0.48484848, 0.45454545, 0.64646465, 0.        , 0.03030303,\n",
       "       0.83838384, 0.96969697, 0.07070707, 0.41414141, 0.11111111,\n",
       "       0.55555556, 0.14141414, 0.38383838, 0.36363636, 0.39393939,\n",
       "       0.93939394, 0.48484848, 0.02020202, 0.07070707, 0.51515152,\n",
       "       0.35353535, 0.76767677, 0.01010101, 0.96969697, 0.17171717,\n",
       "       0.81818182, 0.78787879, 0.12121212, 0.02020202, 1.        ])"
      ]
     },
     "execution_count": 241,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x - np.min(x)) / (np.max(x) - np.min(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50, 2)"
      ]
     },
     "execution_count": 244,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = np.random.randint(0, 100, size = (50, 2))\n",
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array(X, dtype = float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "metadata": {},
   "outputs": [],
   "source": [
    "X[:, 0] = (X[:, 0] - np.min(X)) / (np.max(X[:, 0]) - np.min(X[:, 0]))\n",
    "X[:, 1] = (X[:, 1] - np.min(X)) / (np.max(X[:, 1]) - np.min(X[:, 1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 247,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.75531915, 0.99988918],\n",
       "       [0.45744681, 0.91655585],\n",
       "       [0.74468085, 0.61447252],\n",
       "       [0.17021277, 1.03113918],\n",
       "       [0.26595745, 0.64572252],\n",
       "       [0.76595745, 0.16655585],\n",
       "       [0.58510638, 0.56238918],\n",
       "       [0.59574468, 0.17697252],\n",
       "       [0.03191489, 0.61447252],\n",
       "       [0.69148936, 0.34363918],\n",
       "       [0.64893617, 0.39572252],\n",
       "       [0.29787234, 0.13530585],\n",
       "       [0.82978723, 0.59363918],\n",
       "       [0.5       , 0.72905585],\n",
       "       [0.30851064, 0.19780585],\n",
       "       [0.44680851, 0.34363918],\n",
       "       [0.11702128, 1.02072252],\n",
       "       [0.11702128, 0.06238918],\n",
       "       [0.69148936, 0.40613918],\n",
       "       [0.35106383, 0.36447252],\n",
       "       [0.27659574, 0.76030585],\n",
       "       [0.92553191, 0.17697252],\n",
       "       [0.28723404, 0.20822252],\n",
       "       [0.09574468, 0.49988918],\n",
       "       [0.42553191, 0.87488918],\n",
       "       [0.61702128, 0.90613918],\n",
       "       [0.73404255, 0.93738918],\n",
       "       [0.19148936, 0.30197252],\n",
       "       [0.06382979, 0.23947252],\n",
       "       [0.20212766, 0.82280585],\n",
       "       [0.86170213, 0.33322252],\n",
       "       [0.59574468, 0.15613918],\n",
       "       [0.12765957, 0.85405585],\n",
       "       [0.95744681, 0.24988918],\n",
       "       [0.59574468, 0.64572252],\n",
       "       [0.57446809, 0.98947252],\n",
       "       [0.30851064, 0.99988918],\n",
       "       [0.21276596, 0.81238918],\n",
       "       [0.86170213, 0.38530585],\n",
       "       [1.0106383 , 0.03113918],\n",
       "       [0.5106383 , 0.43738918],\n",
       "       [0.69148936, 0.54155585],\n",
       "       [0.40425532, 0.14572252],\n",
       "       [0.0106383 , 0.40613918],\n",
       "       [0.78723404, 0.07280585],\n",
       "       [0.18085106, 0.08322252],\n",
       "       [0.44680851, 0.45822252],\n",
       "       [0.62765957, 0.78113918],\n",
       "       [0.04255319, 0.14572252],\n",
       "       [0.60638298, 0.53113918]])"
      ]
     },
     "execution_count": 247,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 249,
   "metadata": {},
   "outputs": [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suHe/RI5WOJiFuBnZn5bBMTW4Cpx7pWZvo0cLTBeS1KlXP7MeB7wMPAeyLivobmtghVjvc7wDuATwHfXxvXS5l5ocKmh43Hqq4G9qF9uEelY4mIq4DPAY3U6RakyrEeBR7LzPONzWpxqhzvLcDxzDwLfAW4o6G5LUKV4/0M8InM/DvgB8DHG5pbWxqPVV0NhkP7cI+px7KWxX4NuD8zZ91ArQuqnLf3AZ+MiKeAd0XEl5qZ2kJUOd4fAtevfT1i9g3yuqDK8e4EboqIbcB7gdIX0zQfqzKzc/8BvwU8BzzK6qXazcCDU8a8te15L/h4/xx4BXhq7b972p73oo51w/in2p5zA+f2SlbftJ8GngH2tj3vBR/ve4DnWc1kvwVc0fa8azjup9b+fyfwlxu+94614/0H4H9Yvbm80Pl0duXp2t3mu4Cnc/USdaYxfVHSsUwzpGMFj7ft+XTB2qfL3QY8kQ18EFFnA7skaTZdrbFLkmZkYJekwhjYJakwBnZJKoyBXZIK8/9LWEo61c+KVAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X[:, 0], X[:, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 250,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.47212765957446806"
      ]
     },
     "execution_count": 250,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X[:, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2745413888587092"
      ]
     },
     "execution_count": 251,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(X[:, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 252,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5021808510638298"
      ]
     },
     "execution_count": 252,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X[:, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3050023480100877"
      ]
     },
     "execution_count": 253,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(X[:, 1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 均值标准差归一化Standardization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 255,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50, 2)"
      ]
     },
     "execution_count": 255,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X2 = np.random.randint(0, 100, size = (50, 2))\n",
    "X2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 256,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2 = np.array(X2, dtype = float)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "metadata": {},
   "outputs": [],
   "source": [
    "X2[:, 0] = (X2[:, 0] - np.mean(X2[:, 0])) / np.std(X2[:, 0])\n",
    "X2[:, 1] = (X2[:, 1] - np.mean(X2[:, 1])) / np.std(X2[:, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 259,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.12873338, -0.11856835],\n",
       "       [ 1.63901322,  1.46000436],\n",
       "       [-0.05106184, -1.03063259],\n",
       "       [-1.27366934, -0.32904471],\n",
       "       [-1.05791507, -0.9604738 ],\n",
       "       [ 0.23661051, -0.3992035 ],\n",
       "       [-0.55448846, -1.13587077],\n",
       "       [-0.05106184, -0.78507683],\n",
       "       [ 0.95579139,  0.30238437],\n",
       "       [ 1.49517705,  0.33746377],\n",
       "       [-1.45346456,  1.46000436],\n",
       "       [-0.19489802,  0.58301952],\n",
       "       [-1.02195603, -0.36412411],\n",
       "       [ 0.70407808, -1.31126773],\n",
       "       [-0.9140789 , -1.03063259],\n",
       "       [-0.0151028 ,  1.38984558],\n",
       "       [ 1.06366852, -1.20602955],\n",
       "       [ 1.31538183, -0.43428289],\n",
       "       [ 1.81880844,  0.75841649],\n",
       "       [-0.95003794, -0.08348896],\n",
       "       [ 0.63215999, -0.3992035 ],\n",
       "       [ 1.06366852, -1.17095016],\n",
       "       [-0.69832463,  0.79349588],\n",
       "       [-1.66921882, -1.24110895],\n",
       "       [ 0.34448764,  0.47778134],\n",
       "       [-0.5904475 , -0.9604738 ],\n",
       "       [-0.05106184,  0.61809892],\n",
       "       [-1.27366934, -0.32904471],\n",
       "       [-0.98599698, -1.31126773],\n",
       "       [ 1.71093131,  0.51286074],\n",
       "       [-1.02195603,  1.67048073],\n",
       "       [-1.09387412,  1.60032194],\n",
       "       [ 1.49517705, -0.99555319],\n",
       "       [ 1.09962756,  0.61809892],\n",
       "       [-0.33873419,  0.61809892],\n",
       "       [-0.44661133,  0.37254316],\n",
       "       [-1.30962838,  1.56524254],\n",
       "       [ 1.67497227,  1.46000436],\n",
       "       [ 1.24346374,  1.42492497],\n",
       "       [-0.26681611, -0.46936229],\n",
       "       [-1.52538264,  1.53016315],\n",
       "       [ 0.02085625, -0.9604738 ],\n",
       "       [ 0.3085286 , -0.25888593],\n",
       "       [-0.80620177, -0.71491804],\n",
       "       [-0.15893897,  1.38984558],\n",
       "       [ 1.42325896, -1.38142652],\n",
       "       [ 0.23661051, -1.38142652],\n",
       "       [-1.12983316,  0.19714619],\n",
       "       [ 0.20065147,  1.00397225],\n",
       "       [ 0.09277433, -1.38142652]])"
      ]
     },
     "execution_count": 259,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 262,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX8AAAD6CAYAA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vaQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X2[:, 0], X2[:, 1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-4.718447854656915e-17"
      ]
     },
     "execution_count": 263,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X2[:, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-7.993605777301127e-17"
      ]
     },
     "execution_count": 265,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.mean(X2[:, 1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 266,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(X2[:, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 267,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.std(X2[:, 1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 调用自己实现的py脚本完成数据归一化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 准备数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "iris = datasets.load_iris()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = iris.data\n",
    "y = iris.target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2],\n",
       "       [4.9, 3. , 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.3, 0.2],\n",
       "       [4.6, 3.1, 1.5, 0.2],\n",
       "       [5. , 3.6, 1.4, 0.2],\n",
       "       [5.4, 3.9, 1.7, 0.4],\n",
       "       [4.6, 3.4, 1.4, 0.3],\n",
       "       [5. , 3.4, 1.5, 0.2],\n",
       "       [4.4, 2.9, 1.4, 0.2],\n",
       "       [4.9, 3.1, 1.5, 0.1]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:10, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state = 666)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(112, 4)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(112,)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(38, 4)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(38,)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from  common.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "myStandardScaler = StandardScaler()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<common.preprocessing.StandardScaler at 0x16a31eac4e0>"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "myStandardScaler.fit(np.array(X_train))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train_standard = myStandardScaler.transform(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_test_standard = myStandardScaler.transform(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "knn_clf = KNeighborsClassifier(n_neighbors=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=None, n_neighbors=3, p=2,\n",
       "           weights='uniform')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.fit(X_train_standard, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9736842105263158"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.score(X_test_standard, y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用scikit-learn中的Scaler将数据集归一化"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用scikit-learn中的StandardScaler（均值标准差归一化）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "standardScaler = StandardScaler()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StandardScaler(copy=True, with_mean=True, with_std=True)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "standardScaler.fit(X_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5.825     , 3.09285714, 3.68571429, 1.16428571])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 均值\n",
    "standardScaler.mean_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.80239597, 0.4493476 , 1.75828941, 0.75543946])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 标准差\n",
    "standardScaler.scale_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.77891718,  2.24134469, -1.24309131, -1.40882992],\n",
       "       [ 0.46735031, -1.98700771,  0.46311245,  0.44439602],\n",
       "       [ 1.83824454, -0.42919366,  1.48683471,  0.84151586],\n",
       "       [ 0.7166038 ,  0.23844092,  0.91810012,  1.50338227],\n",
       "       [ 0.21809681,  0.68353065,  0.46311245,  0.5767693 ],\n",
       "       [-0.77891718, -0.87428339,  0.1218717 ,  0.31202273],\n",
       "       [-0.52966368,  1.35116524, -1.24309131, -1.27645664],\n",
       "       [-0.65429043,  1.35116524, -1.24309131, -1.27645664],\n",
       "       [-1.02817067,  0.90607551, -1.18621785, -0.74696352],\n",
       "       [-1.77593116, -0.42919366, -1.29996477, -1.27645664]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train_standard = standardScaler.transform(X_train)\n",
    "X_train_standard[:10, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.28041018, -0.2066488 ,  0.46311245,  0.44439602],\n",
       "       [-0.03115669, -0.65173853,  0.8043532 ,  1.63575555],\n",
       "       [-1.02817067, -1.76446284, -0.21936906, -0.21747039],\n",
       "       [-0.03115669, -0.87428339,  0.8043532 ,  0.97388914],\n",
       "       [-1.52667767,  0.01589606, -1.24309131, -1.27645664],\n",
       "       [-0.40503693, -1.31937312,  0.17874516,  0.17964945],\n",
       "       [-0.15578344, -0.65173853,  0.46311245,  0.17964945],\n",
       "       [ 0.84123055, -0.2066488 ,  0.86122666,  1.10626242],\n",
       "       [ 0.59197705, -1.76446284,  0.40623899,  0.17964945],\n",
       "       [-0.40503693, -1.09682825,  0.40623899,  0.04727617]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_test_standard = standardScaler.transform(X_test)\n",
    "X_test_standard[:10, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "knn_clf = KNeighborsClassifier(n_neighbors = 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',\n",
       "           metric_params=None, n_jobs=None, n_neighbors=3, p=2,\n",
       "           weights='uniform')"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.fit(X_train_standard, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9736842105263158"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.score(X_test_standard, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.34210526315789475"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn_clf.score(X_test, y_test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "327.448px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
